"""
--coding:utf-8--
@File: cli.py
@Author:frank yu
@DateTime: 2020.08.19 15:46
@Contact: frankyu112058@gmail.com
@Description:module to combine and Cmd and argparse
"""
import argparse
import sys
from cmd import Cmd

from hello import Hello


class Cli(Cmd):
    def __init__(self):
        """
        function: __init__
        :return: None
        """
        title = \
            '''
        +++                +++++        ++++++++++++    ++++       ++++    
        +++               +++ +++       +++++++++++++      +++   +++
        +++              +++   +++      +++        +++        +++
        +++             +++++++++++     +++        +++        +++
        +++++++++++    +++       +++    +++++++++++++         +++
        +++++++++++   +++         +++   +++++++++++           +++
        
        Welcome to LADY, a Command and Logging Frame made by lady_killer9(https://blog.csdn.net/lady_killer9)
        version 1.0.0
        use hello -h/--help to show options
        Hit '<ctrl-c>' or 'exit' to shutdown LADY.
            '''
        display = "\033[1;36;40m{}\033[0m".format(title)
        self.do_clear(None)
        print(display)
        Cmd.__init__(self)
        self.prompt = 'lady_killer>'

    def emptyline(self):
        """
        Called when an empty line is entered in response to the prompt.
        If this method is not overridden, it repeats the last nonempty
        command entered.
        """
        return

    def do_hello(self, cmd):
        """
        function:run hello module to test program
        :param cmd: command from console
        :return: None
        """
        commond = cmd.split()
        # print(commond)
        parse = argparse.ArgumentParser(description='Process some integers.')
        parse.prog = "hello"
        parse.usage = parse.prog + " -l [-d] [-i] [-w] [-e] [-c]"
        parse.add_argument('-d', '--debug', type=str, default="this is debug message",
                           help="print message with logging.DEBUG")
        parse.add_argument('-i', '--info', type=str, default="this is info message",
                           help="print message with logging.INFO")
        parse.add_argument('-w', '--warning', type=str, default="this is warning message",
                           help="print message with logging.WARN")
        parse.add_argument('-e', '--error', type=str, default="this is error message",
                           help="print message with logging.ERROR")
        parse.add_argument('-c', '--critical', type=str, default="this is critical message",
                           help="print message with logging.CRITICAL")
        parse.add_argument('-l', type=str, choices=["debug", "info", "warn", "error", "critical"],
                           help="set log level(debug info warn error critical)")
        args = parse.parse_args(commond)
        # print(args)
        kwargs = dict()
        kwargs['d_info'] = args.debug
        kwargs['i_info'] = args.info
        kwargs['w_info'] = args.warning
        kwargs['e_info'] = args.error
        kwargs['c_info'] = args.critical
        kwargs['l'] = args.l
        hello = Hello()
        hello.set_log(**kwargs)
        hello.debug(**kwargs)
        hello.info(**kwargs)
        hello.warning(**kwargs)
        hello.error(**kwargs)
        hello.critical(**kwargs)

    def do_exit(self, msg):
        """
        function: close the cmd
        :param msg: param about service to stop
        :return: None
        """
        print('Bye!!!')
        sys.exit(0)

    def do_clear(self, msg):
        """
        function: clear screen
        """
        print("\033c")
